package com.kensure.shike.user.service;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import co.kensure.exception.BusinessExceptionUtil;
import co.kensure.frame.JSBaseService;
import co.kensure.mem.CollectionUtils;
import co.kensure.mem.MapUtils;
import co.kensure.mem.Utils;

import com.kensure.basekey.BaseKeyService;
import com.kensure.shike.user.dao.SKLoginDao;
import com.kensure.shike.user.model.SKLogin;
import com.kensure.shike.user.model.SKUser;

/**
 * 试客登录表服务实现类
 * 
 * @author fankd created on 2018-9-2
 * @since
 */
@Service
public class SKLoginService extends JSBaseService {

	@Resource
	private SKLoginDao dao;

	@Resource
	private BaseKeyService baseKeyService;

	@Resource
	private SKUserService sKUserService;

	public SKLogin selectOne(Long id) {
		return dao.selectOne(id);
	}

	public List<SKLogin> selectByWhere(Map<String, Object> parameters) {
		return dao.selectByWhere(parameters);
	}

	public long selectCountByWhere(Map<String, Object> parameters) {
		return dao.selectCountByWhere(parameters);
	}

	public boolean insert(SKLogin obj) {
		obj.setId(baseKeyService.getKey("sk_login"));
		obj.setStatus(0L);
		super.beforeInsert(obj);
		return dao.insert(obj);
	}

	public boolean update(SKLogin obj) {
		super.beforeUpdate(obj);
		return dao.update(obj);
	}

	public boolean updateByMap(Map<String, Object> params) {
		return dao.updateByMap(params);
	}

	/**
	 * 根据令牌拿到用户登录信息 tokenid = sessionid
	 * 
	 * @return
	 */
	public SKLogin selectByTokenId(String tokenid) {
		Map<String, Object> parameters = MapUtils.genMap("sessionid", tokenid);
		List<SKLogin> loginlist = selectByWhere(parameters);
		SKLogin u = null;
		if (CollectionUtils.isNotEmpty(loginlist)) {
			u = loginlist.get(0);
		}
		return u;
	}

	// 开始

	/**
	 * 登录逻辑
	 * 
	 * @return
	 */
	@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
	public String login(SKUser user) {
		if (user == null) {
			BusinessExceptionUtil.threwException("用户不存在");
		}
		if (user.getStatus() == -1) {
			BusinessExceptionUtil.threwException("账号已停用");
		}

		SKLogin login = selectByUserId(user.getId());
		if (login == null) {
			// 会话id
			String uuid = Utils.getUUID();
			login = new SKLogin();
			login.setSessionid(uuid);
			login.setUserid(user.getId());
			insert(login);
		}
		return login.getSessionid();
	}

	/**
	 * 根据userid拿到用户登录信息
	 * 
	 * @return
	 */
	public SKLogin selectByUserId(Long userid) {
		Map<String, Object> parameters = MapUtils.genMap("userid", userid);
		List<SKLogin> loginlist = selectByWhere(parameters);
		SKLogin u = null;
		if (CollectionUtils.isNotEmpty(loginlist)) {
			u = loginlist.get(0);
		}
		return u;
	}

	/**
	 * add by fankd 用spring的框架在本地线程变量中获取 token,有可能获取不到
	 * 
	 * @return
	 */
	public static String getTokenId() {
		String tokenId = null;
		RequestAttributes ras = RequestContextHolder.getRequestAttributes();
		if (ras != null) {
			HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
			if (request != null) {
				tokenId = request.getHeader("tokenid");
			}
			if (StringUtils.isBlank(tokenId) || tokenId.length() < 20) {
				tokenId = null;
			}
		}
		return tokenId;
	}

}
